<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <!-- value-format就是连着后端给的数据格式也会改变 -->
        <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="选择日期" format="yyyy年-MM月-dd日" value-format="yyyy-MM-dd" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择活动区域">
          <!-- 循环 ConstantEnum组件里面的hireType, 且显示到页面需要:label与hireType的value搭配,:value是是为了响应后端给的1与二-->
          <el-option v-for="item in ConstantEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />

        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" readonly @focus="isShowTree=true" />
        <el-tree v-if="isShowTree" :data="data" :props="defaultProps" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" type="date" format="yyyy年-MM月-dd日" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="quxiao">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import ConstantEnum from '@/constant/employees'
import { validatePassas } from '@/utils/validate'
import { getDepartmentjia } from '@/api/department'
import { transformListToTree2 } from '@/utils'
import { getUserRequest } from '@/api/emPloyees'

export default {

  data() {
    const validatePass = (rule, value, callback) => {
      if (!validatePassas(value)) {
        callback(new Error('密码格式不对'))
      } else {
        callback()
      }
    }
    return {

      ConstantEnum: ConstantEnum,
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''

      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePass, trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      isShowTree: false
    }
  },
  created() {
    this.qinQIU()
  },
  methods: {
    // 树形结构图
    async qinQIU() {
      try {
        const res = await getDepartmentjia()
        console.log('22222222222222222', res)

        // 数组第一条数据.shift()
        res.data.depts.shift()
        this.data = transformListToTree2(res.data.depts, '')
      } catch (err) {
        console.log('错误')
      }
    },
    // 新增员工的请求
    async getRequest() {
      try {
        const res = await getUserRequest(this.form)
        // console.log(res)
        if (res.success) {
          // 成功之后步骤
          this.$emit('addgetsuccess')
        } else {
          // 失败的提醒
          this.$message.error('新增没有')
        }
      } catch (err) {
        console.log('新增请求错误 ', err)
      }
    },
    onSubmit() {
      console.log(this.form)
      this.$refs.formRef.validate(val => {
        console.log(val)
        // 就是所有的表单校验好了
        if (val) {
          this.getRequest()
        }
      })
    },
    // 点击取消通知父组件给模态框关闭
    quxiao() {
      this.$emit('close')
    },
    handleNodeClick(val) {
      console.log(val, 5555555555)
      // 优化,因为有些是子级的所以当children的大于没有的children的就会被返回,就是说有子级的不能直接点击
      if (val.children.length > 0) {
        return
      }
      this.form.departmentName = val.name
      this.isShowTree = false
    }

  }
}
</script>
